package com.example.rabbitmqspringboot.demos.web.listener;

import com.example.rabbitmqspringboot.demos.web.pojo.Stud;
import com.example.rabbitmqspringboot.demos.web.pojo.User;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @ClassName: MsgConsumer
 * @Description:
 * @Date 2024/03/28 09:11
 * @Author songshaoqiu
 **/
@Component
@Slf4j
@RabbitListener(queues = {"hello-java-queue"})
public class MsgConsumer {

    @RabbitHandler
    public void handleDirectQueueForUser(Message message, User user,Channel channel) throws IOException {
        log.info("接收到消息【{}】",message);
        log.info("消息内容【{}】", user.getName());
        //如果设置了手动确认模式
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitHandler
    public void handleDirectQueue(Message message, Stud stud, Channel channel)  {
        log.info("接收到消息【{}】",message);
        log.info("消息内容【{}】", stud.getStudName());
        log.info("消息是否重发【{}】",message.getMessageProperties().getRedelivered());
        try {
            int i = 1/0;
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);
        }catch (Exception e){
            log.info("模拟消息消费失败===>");
        }
    }


}
